$(document).ready(function () {
    //1.单机按钮查询全部
    $("[value='查询全部']").click(function () {
        $.ajax({
            type:"POST",
            url:"ajaxgetall",
            success:function (depts) {
                $("tbody").empty();
                for (var i=0;i<depts.length;i++){
                    var obj = "    <tr >\n" +
                        "            <td >"+depts[i].deptno+"</td>\n" +
                        "            <td >"+depts[i].dname+"</td>\n" +
                        "            <td >"+depts[i].loc+"</td>\n" +
                        "        </tr>";
                    $(obj).appendTo("tbody")
                }
            }
        })
    })

    //2.窗体加载后，显示分页第一页内容
    pageshow(1);

    $("[value='按条件查询']").click(function () {
        pageshow(1)
    })
})

//创建分页公用方法
function pageshow(indexpage) {
    var queryname=$("#queryid").val();
    $.ajax({
        type:"GET",
        url:"getpageone",
        data:{indexpage:indexpage,queryname:queryname},
        success:function (pageinfo) {
            $("tbody").empty();
            for (var i=0;i<pageinfo.list.length;i++){
                var obj = "    <tr >\n" +
                    "            <td >"+pageinfo.list[i].deptno+"</td>\n" +
                    "            <td >"+pageinfo.list[i].dname+"</td>\n" +
                    "            <td >"+pageinfo.list[i].loc+"</td>\n" +
                    "        </tr>";
                $(obj).appendTo("tbody")
            }

            //添加分页链接
            $("tfoot td").empty();
            if(!pageinfo.isFirstPage){
                var objspan="<span onclick='pageshow(1);'>首页</span>"+
                    "<span onclick='pageshow("+pageinfo.prePage+")'>上一页</span>";
                $(objspan).appendTo("tfoot td");
            }
            if(!pageinfo.isLastPage){
                var objspan="<span onclick='pageshow("+pageinfo.nextPage+")'>下一页</span>"+
                    "<span onclick='pageshow("+pageinfo.pages+")'>尾页</span>";
                $(objspan).appendTo("tfoot td");
            }
        }
    })
}
